package com.example.administrator.yuejian2.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.activity.TrainDetailActivity;
import com.example.administrator.yuejian2.bean.TrainBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.administrator.yuejian2.other.Url.IMAGEURL;

public class TrainAdapter extends RecyclerView.Adapter {


    private Context context;
    private LayoutInflater layoutInflater;
    private List<TrainBean> list;

    public TrainAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void getTrainList(List<TrainBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.train_item, parent, false);
        return new TrainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TrainViewHolder trainViewHolder = (TrainViewHolder) holder;
        final TrainBean bean = list.get(position);
            trainViewHolder.trainName.setText(bean.getTrain_name());
            trainViewHolder.trainAddress.setText(bean.getTrain_address());
        Picasso.with(context).load(IMAGEURL+bean.getTrain_pic()).into(trainViewHolder.trainPic);
            trainViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, TrainDetailActivity.class);
                    intent.putExtra("trainid",bean.getTrain_id());
                    context.startActivity(intent);
                }
            });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }


    class TrainViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.train_pic)
        ImageView trainPic;
        @BindView(R.id.train_name)
        TextView trainName;
        @BindView(R.id.train_address)
        TextView trainAddress;

        public TrainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
